package com.school.graduation.goods.model;

import com.school.graduation.bean.BigCatogaryBean;
import com.school.graduation.bean.FindResultBean;
import com.school.graduation.bean.GoodsBean;
import com.school.graduation.internet.CatogaryInterface;
import com.school.graduation.internet.FoundGoodDetailInterface;
import com.school.graduation.model.Goods;
import com.school.graduation.model.GoodsType;
import com.school.graduation.model.Student;
import com.school.graduation.util.DateUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class FoundGoodDetailModel implements ImplFoundGoodDetailModel {

    @Override
    public void getGoodsByInputNew(String input, final FoundGoodDetailListener listener) {
        Retrofit retrofit  =new Retrofit.Builder()
                .baseUrl("http://123.206.20.29:3000/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FoundGoodDetailInterface foundGood = retrofit.create(FoundGoodDetailInterface.class);
        Call<FindResultBean> call = foundGood.getFindGoodResult(input,0);
        call.enqueue(new Callback<FindResultBean>() {
            @Override
            public void onResponse(Call<FindResultBean> call, Response<FindResultBean> response) {
                if (response.isSuccessful()){
                    FindResultBean bean = response.body();
                    listener.getGoodsesNew(bean.getGoods());
                }else {
                    try {
                        System.out.println(response.errorBody().string());
                        listener.getError("response error "+new String(response.errorBody().bytes()));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<FindResultBean> call, Throwable t) {
                listener.getError("failure "+t.toString());
            }
        });
    }

    @Override
    public void getGoodsBySortNew(String sort, final FoundGoodDetailListener listener) {
        Retrofit retrofit  = new Retrofit.Builder()
                .baseUrl("http://123.206.20.29:3000/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CatogaryInterface catogary = retrofit.create(CatogaryInterface.class);
        Call<BigCatogaryBean> call = catogary.getCategory(sort);
        call.enqueue(new Callback<BigCatogaryBean>() {
            @Override
            public void onResponse(Call<BigCatogaryBean> call, Response<BigCatogaryBean> response) {
                if (response.isSuccessful()){
                    BigCatogaryBean catogaryBean = response.body();
                    List<GoodsBean> goodsBeens = catogaryBean.getGoods();
                    listener.getGoodsesNew(goodsBeens);
                }else {
                    listener.getError("服务器出错");
                }
            }

            @Override
            public void onFailure(Call<BigCatogaryBean> call, Throwable t) {
                listener.getError(t.toString());
            }
        });
    }
}
